// initSession_ID();// 初始化session_id
var PAGE_INDEX = 1;
$(document).ready(function () {
    var path = "user/code/list";  //提货码列表
    var params = {
        "SESSION_ID":SESSION_ID,
        "STATE": 1,
        "PAGE_CAPACITY":50,
        "PAGE_INDEX":PAGE_INDEX
    }
    request(path, params, loadCouponList);
});
function loadCouponList(res) {
    var couponDom =
        '<div class="coupon-item">\
            <img class="coupon-img" src="../assets/img/share_coupon.png" alt="图片加载错误">\
            <div class="product-info"  orderid="%s">\
                <div class="coupon-categ">\
                    <div class="coupon-title">%s</div>\
                    <div class="coupon-date">获取时间:<sapn id="dead_line">%s</sapn></div>\
                </div>\
            </div>\
            <div class="product-num" orderid="%s">\
                <div class="coupon-price"><p class="privilige">%s杯</p></div>\
            </div>\
        </div>';
    
    if(PAGE_INDEX == 1 && res.BODY.COUNT != 0) {
        $(".none-coupon-display").css("display","none");
        $(".content").empty();
    } else {
        $(".none-coupon-display").css("display","block");
    }
    var listTimer = null;
    _.each(res.BODY.LIST,function (item, index) {
        //获取订单提货码
        var getCodePath = "user/code/get";
        var getCodeParams = {
            "ORDER_NO":item.ORDER_NO
        };
        var orderId = item.ORDER_NO;
        var name = item.GOODS;
        var getTime = item.USE_START;
        var count = item.COUNT;
        var arg = {};
        arg.async  = true;
        request(getCodePath,getCodeParams,function (res) {
            var flg = /^0+$/ig.test(res.RSPCD); //全零true
            if(!flg) return 0;
            _.each(res.BODY.LIST,function (item1, index) {
                if(item1.STATE != 1) {
                    count--;
                }
            });
            if(count != 0) {
                $(".content").append(sprintf(couponDom,orderId,name,getTime,orderId,count));
            }
        },arg)
    })
}
$("#edit_data").on("tap",function () {
    location.href = './rule.html';
})

//去制作咖啡s
$("#share_coupon_content").on("tap",".product-info",function (e) {
    if(IS_BIND != "1") {
        location.replace("./register.html");
    } else {
        var orderId =  e.currentTarget.attributes.orderid.value;
        location.replace("./order_make.html?order_id="+orderId);
    }
})
//去分享券
$("#share_coupon_content").on("tap",".product-num",function (e) {
    var orderId =  e.currentTarget.attributes.orderid.value;
    //订单详情查询
    location.replace("./pay_success.html?order_id="+orderId + "&nowToShare");
})
$("#buy_coffee").on("tap",function () {
    location.replace("./index.html");
});
$("#treat").on("tap",function () {
    location.replace("./index.html?isShare");
});
